﻿using System.Windows;
//using System.Windows.Forms;
using System;
using System.Text.RegularExpressions;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace InteractiveMapApp
{
    /// <summary>
    /// Interaction logic for ManageAppSettings.xaml
    /// </summary>
    public partial class ManageAppSettings : Window
    {
        bool unsavedChanges = false;

        public ManageAppSettings()
        {
            InitializeComponent();
        }

        private void Window_manageAppSettings_Loaded(object sender, RoutedEventArgs e)
        {
            populateFieldsWithCurrentValues();
        }

        private void populateFieldsWithCurrentValues()
        {
            txtRequestPageURL.Text = Properties.Settings.Default.RequestPageURL;
            txtStarterURL.Text = Properties.Settings.Default.iconsPathStarterURL;
            txtStoryCycleInterval.Text = Properties.Settings.Default.storyCycleInterval.ToString(); //int
            txtMaxStories.Text = Properties.Settings.Default.MaxNumberRandomStoriesToShowAtATime.ToString(); //int
            txtMapDefaultZoomLevel.Text = Properties.Settings.Default.MapDefaultZoom.ToString(); //int
            txtMapMinZoom.Text = Properties.Settings.Default.MapMinZoom.ToString(); //int
            txtMapMaxZoom.Text = Properties.Settings.Default.MapMaxZoom.ToString(); //int
            txtcityBoundTopLeftLattitude.Text = Properties.Settings.Default.cityBoundTopLeftLattitude.ToString(); //double
            txtcityBoundTopLeftLongitude.Text = Properties.Settings.Default.cityBoundTopLeftLongitude.ToString(); //double
            txtMapCenterLattitude.Text = Properties.Settings.Default.mapCenterLattitude.ToString(); //double
            txtMapCenterLongitude.Text = Properties.Settings.Default.mapCenterLongitude.ToString(); //double
            txtcityBoundBottomRightLattitude.Text = Properties.Settings.Default.cityBoundBottomRightLattitude.ToString(); //double
            txtcityBoundBottomRightLongitude.Text = Properties.Settings.Default.cityBoundBottomRightLongitude.ToString(); //double
            dlNewStoriesIntervalTxtbox.Text = Properties.Settings.Default.fetchRecentStoriesInterval.ToString(); //int
            userstorylifetimeTxtbox.Text = Properties.Settings.Default.UserStoryLifetime.ToString();
            screenWidthTxtbox.Text = Properties.Settings.Default.XSize.ToString();
            screenHeightTxtbox.Text = Properties.Settings.Default.YSize.ToString();
            hostnameTxtbox.Text = Properties.Settings.Default.messageQueueHostName;
            maxMuseumStoriesOnScreenTxt.Text = Properties.Settings.Default.MaxNumberMuseumStoriesToShowAtATime.ToString();
            maxUserStoriesOnScreenTxt.Text = Properties.Settings.Default.MaxNumberMyStoriesToShowAtATime.ToString();
            messageQueueNameTxtBox.Text =  Properties.Settings.Default.messageQueueName;
            //scrlTopLatTxt.Text = Properties.Settings.Default.scrollBoundTopLat.ToString();
            //scrlBottomLatTxt.Text = Properties.Settings.Default.scrollBoundBottomLat.ToString();
            //scrlLeftLngTxt.Text = Properties.Settings.Default.scrollBoundLeftLng.ToString();
            //scrlRightLngTxt.Text = Properties.Settings.Default.scrollBoundRightLng.ToString();
            
            if (Properties.Settings.Default.OrientationPortrait)
            {
                portraitRadio.IsChecked = true;
                landscapeRadio.IsChecked = false;
                widthLbl.Content = "Screen Res. Height (Pixels)";
                heightLbl.Content = "Screen Res. Width (Pixels)";
            }
            else
            {
                portraitRadio.IsChecked = false;
                landscapeRadio.IsChecked = true;
                widthLbl.Content = "Screen Res. Width (Pixels)";
                heightLbl.Content = "Screen Res. Height (Pixels)";
            }

            if (Properties.Settings.Default.ProjectorMode)
            {
                tableRadio.IsChecked = true;
                touchRadio.IsChecked = false;

                //textboxes
                txtMapMaxZoom.Visibility = Visibility.Collapsed;
                txtMapMinZoom.Visibility = Visibility.Collapsed;
                //scrlTopLatTxt.Visibility = Visibility.Collapsed;
                //scrlBottomLatTxt.Visibility = Visibility.Collapsed;
                //scrlLeftLngTxt.Visibility = Visibility.Collapsed;
                //scrlRightLngTxt.Visibility = Visibility.Collapsed;
                txtcityBoundTopLeftLattitude.Visibility = Visibility.Collapsed;
                txtcityBoundTopLeftLongitude.Visibility = Visibility.Collapsed;
                txtcityBoundBottomRightLattitude.Visibility = Visibility.Collapsed;
                txtcityBoundBottomRightLongitude.Visibility = Visibility.Collapsed;
                txtMaxStories.Visibility = Visibility.Collapsed;
                userstorylifetimeTxtbox.Visibility = Visibility.Collapsed;

                //labels
                //scrlRestrictionLbl.Visibility = Visibility.Collapsed;
                //scrlTopLatLbl.Visibility = Visibility.Collapsed;
                //scrlBottomLatLbl.Visibility = Visibility.Collapsed;
                //scrlLeftLngLbl.Visibility = Visibility.Collapsed;
                //scrlRightLngLbl.Visibility = Visibility.Collapsed;
                lblMapMaxZoom.Visibility = Visibility.Collapsed;
                lblMapMinZoom.Visibility = Visibility.Collapsed;
                lblcityBoundTopLeftLattitude.Visibility = Visibility.Collapsed;
                lblcityBoundTopLeftLongitude.Visibility = Visibility.Collapsed;
                lblcityBoundBottomRightLattitude.Visibility = Visibility.Collapsed;
                lblcityBoundBottomRightLongitude.Visibility = Visibility.Collapsed;
                lblMaxStories.Visibility = Visibility.Collapsed;
                userstorylifetimeLbl.Visibility = Visibility.Collapsed;

                //rectangle5.Visibility = Visibility.Collapsed;
                rectangle3.Height = stackPanel1.ActualHeight + 8;
            }
            else
            {
                tableRadio.IsChecked = false;
                touchRadio.IsChecked = true;

                //textboxes
                txtMapMaxZoom.Visibility = Visibility.Visible;
                txtMapMinZoom.Visibility = Visibility.Visible;
                //scrlTopLatTxt.Visibility = Visibility.Visible;
                //scrlBottomLatTxt.Visibility = Visibility.Visible;
                //scrlLeftLngTxt.Visibility = Visibility.Visible;
                //scrlRightLngTxt.Visibility = Visibility.Visible;
                txtcityBoundTopLeftLattitude.Visibility = Visibility.Visible;
                txtcityBoundTopLeftLongitude.Visibility = Visibility.Visible;
                txtcityBoundBottomRightLattitude.Visibility = Visibility.Visible;
                txtcityBoundBottomRightLongitude.Visibility = Visibility.Visible;
                txtMaxStories.Visibility = Visibility.Visible;
                userstorylifetimeTxtbox.Visibility = Visibility.Visible;

                //labels
                //scrlRestrictionLbl.Visibility = Visibility.Visible;
                //scrlTopLatLbl.Visibility = Visibility.Visible;
                //scrlBottomLatLbl.Visibility = Visibility.Visible;
                //scrlLeftLngLbl.Visibility = Visibility.Visible;
                //scrlRightLngLbl.Visibility = Visibility.Visible;
                lblMapMaxZoom.Visibility = Visibility.Visible;
                lblMapMinZoom.Visibility = Visibility.Visible;
                lblcityBoundTopLeftLattitude.Visibility = Visibility.Visible;
                lblcityBoundTopLeftLongitude.Visibility = Visibility.Visible;
                lblcityBoundBottomRightLattitude.Visibility = Visibility.Visible;
                lblcityBoundBottomRightLongitude.Visibility = Visibility.Visible;
                lblMaxStories.Visibility = Visibility.Visible;
                userstorylifetimeLbl.Visibility = Visibility.Visible;

                //rectangle5.Visibility = Visibility.Visible;
                rectangle3.Height = stackPanel1.Height + 8;
            }


        }

        //private void Window_manageAppSettings_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        //{
        //    if (unsavedChanges)
        //    {
        //        string message = "Are you sure you want to exit? Unsaved changes will be discarded.";
        //        string caption = "Unsaved Changes";
        //        System.Windows.Forms.MessageBoxButtons buttons = System.Windows.Forms.MessageBoxButtons.OKCancel;
        //        System.Windows.Forms.DialogResult result;
        //        result = System.Windows.Forms.MessageBox.Show(message, caption, buttons);

        //        if (result == System.Windows.Forms.DialogResult.Cancel)
        //        {
        //            e.Cancel = true;
        //        }
        //    }
        //}

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            bool allGood = true;
            string errorMessage = "";
            #region make sure all textboxes in all stackpanels are valid
            foreach (UIElement textbox in stackPanel1.Children)
            {
                if (textbox is TextBox)
                {
                    if ((textbox as TextBox).Tag.Equals("invalidInteger"))
                    {
                        allGood = false;
                        errorMessage += "Only accept Integer numbers for " + (textbox as TextBox).Name + "\n";
                    }
                    if ((textbox as TextBox).Tag.Equals("invalidDouble"))
                    {
                        allGood = false;
                        errorMessage += "Only accept floating numbers for " + (textbox as TextBox).Name + "\n";
                    }
                }
            }

            foreach (UIElement textbox in stackPanel5.Children)
            {
                if (textbox is TextBox)
                {
                    if ((textbox as TextBox).Tag.Equals("invalidInteger"))
                    {
                        allGood = false;
                        errorMessage += "Only accept Integer numbers for " + (textbox as TextBox).Name + "\n";
                    }
                    if ((textbox as TextBox).Tag.Equals("invalidDouble"))
                    {
                        allGood = false;
                        errorMessage += "Only accept floating numbers for " + (textbox as TextBox).Name + "\n";
                    }
                }
            }

            foreach (UIElement textbox in stackPanel3.Children)
            {
                if (textbox is TextBox)
                {
                    if ((textbox as TextBox).Tag.Equals("invalidInteger"))
                    {
                        allGood = false;
                        errorMessage += "Only accept Integer numbers for " + (textbox as TextBox).Name + "\n";
                    }
                    if ((textbox as TextBox).Tag.Equals("invalidDouble"))
                    {
                        allGood = false;
                        errorMessage += "Only accept floating numbers for " + (textbox as TextBox).Name + "\n";
                    }
                }
            }

            foreach (UIElement textbox in stackPanel4.Children)
            {
                if (textbox is TextBox)
                {
                    if ((textbox as TextBox).Tag.Equals("invalidInteger"))
                    {
                        allGood = false;
                        errorMessage += "Only accept Integer numbers for " + (textbox as TextBox).Name + "\n";
                    }
                    if ((textbox as TextBox).Tag.Equals("invalidDouble"))
                    {
                        allGood = false;
                        errorMessage += "Only accept floating numbers for " + (textbox as TextBox).Name + "\n";
                    }
                }
            }

            //foreach (UIElement textbox in stackPanel6.Children)
            //{
            //    if (textbox is TextBox)
            //    {
            //        if ((textbox as TextBox).Tag.Equals("invalidInteger"))
            //        {
            //            allGood = false;
            //            errorMessage += "Only accept Integer numbers for " + (textbox as TextBox).Name + "\n";
            //        }
            //        if ((textbox as TextBox).Tag.Equals("invalidDouble"))
            //        {
            //            allGood = false;
            //            errorMessage += "Only accept floating numbers for " + (textbox as TextBox).Name + "\n";
            //        }
            //    }
            //}
            #endregion

            if (allGood)
            {
                try
                {
                    Properties.Settings.Default.RequestPageURL = txtRequestPageURL.Text;
                    Properties.Settings.Default.iconsPathStarterURL = txtStarterURL.Text;
                    Properties.Settings.Default.storyCycleInterval = Convert.ToInt32(txtStoryCycleInterval.Text); //int
                    Properties.Settings.Default.MapDefaultZoom = Convert.ToInt32(txtMapDefaultZoomLevel.Text); //int
                    Properties.Settings.Default.MapMinZoom = Convert.ToInt32(txtMapMinZoom.Text); //int
                    Properties.Settings.Default.MapMaxZoom = Convert.ToInt32(txtMapMaxZoom.Text); //int
                    Properties.Settings.Default.cityBoundTopLeftLattitude = Convert.ToDouble(txtcityBoundTopLeftLattitude.Text); //double
                    Properties.Settings.Default.cityBoundTopLeftLongitude = Convert.ToDouble(txtcityBoundTopLeftLongitude.Text); //double
                    Properties.Settings.Default.mapCenterLattitude = Convert.ToDouble(txtMapCenterLattitude.Text); //double
                    Properties.Settings.Default.mapCenterLongitude = Convert.ToDouble(txtMapCenterLongitude.Text); //double
                    Properties.Settings.Default.cityBoundBottomRightLattitude = Convert.ToDouble(txtcityBoundBottomRightLattitude.Text); //double
                    Properties.Settings.Default.cityBoundBottomRightLongitude = Convert.ToDouble(txtcityBoundBottomRightLongitude.Text); //double
                    Properties.Settings.Default.fetchRecentStoriesInterval = Convert.ToInt32(dlNewStoriesIntervalTxtbox.Text);
                    Properties.Settings.Default.UserStoryLifetime = Convert.ToInt32(userstorylifetimeTxtbox.Text);
                    Properties.Settings.Default.XSize = Convert.ToInt32(screenWidthTxtbox.Text);
                    Properties.Settings.Default.YSize = Convert.ToInt32(screenHeightTxtbox.Text);
                    Properties.Settings.Default.messageQueueHostName = hostnameTxtbox.Text;
                    Properties.Settings.Default.MaxNumberRandomStoriesToShowAtATime = Convert.ToInt32(txtMaxStories.Text); //int
                    Properties.Settings.Default.MaxNumberMuseumStoriesToShowAtATime = Convert.ToInt32(maxMuseumStoriesOnScreenTxt.Text);
                    Properties.Settings.Default.MaxNumberMyStoriesToShowAtATime = Convert.ToInt32(maxUserStoriesOnScreenTxt.Text);
                    Properties.Settings.Default.messageQueueName = messageQueueNameTxtBox.Text;
                    //Properties.Settings.Default.scrollBoundTopLat = Convert.ToDouble(scrlTopLatTxt.Text);
                    //Properties.Settings.Default.scrollBoundBottomLat = Convert.ToDouble(scrlBottomLatTxt.Text);
                    //Properties.Settings.Default.scrollBoundLeftLng = Convert.ToDouble(scrlLeftLngTxt.Text);
                    //Properties.Settings.Default.scrollBoundRightLng = Convert.ToDouble(scrlRightLngTxt.Text);
                   

                    if (portraitRadio.IsChecked == true)
                    {
                        Properties.Settings.Default.OrientationPortrait = true;
                    }
                    else
                    {
                        Properties.Settings.Default.OrientationPortrait = false;
                    }
                    if (tableRadio.IsChecked == true)
                    {
                        Properties.Settings.Default.ProjectorMode = true;
                    }
                    else
                    {
                        Properties.Settings.Default.ProjectorMode = false;
                    }

                    Properties.Settings.Default.Save();
                    populateFieldsWithCurrentValues();
                    restartToTakeEffectReminder();
                    unsavedChanges = false;
                }
                catch
                {
                    MessageBox.Show("Error. Please double check all the values are valid.");
                }
            }
            else
            {
                MessageBox.Show("There are errors in the input fields, please fix and try again. \n" + errorMessage, "Error" , MessageBoxButton.OK );
            }
        }

        private void btnDefaults_Click(object sender, RoutedEventArgs e)
        {
            //set all back to defaults : 
            Properties.Settings.Default.RequestPageURL = Properties.Settings.Default.defaultRequestPageURL;
            Properties.Settings.Default.iconsPathStarterURL = Properties.Settings.Default.defaulticonsPathStarterURL;
            Properties.Settings.Default.storyCycleInterval = Properties.Settings.Default.defaultStoryCycleInterval;
            Properties.Settings.Default.MaxNumberRandomStoriesToShowAtATime = Properties.Settings.Default.defaultMaxNumberRandomStoriesToShowAtATime;
            Properties.Settings.Default.MapDefaultZoom = Properties.Settings.Default.defaultMapDefaultZoom;
            Properties.Settings.Default.MapMinZoom = Properties.Settings.Default.defaultMapMinZoom;
            Properties.Settings.Default.MapMaxZoom = Properties.Settings.Default.defaultMapMaxZoom;
            Properties.Settings.Default.cityBoundTopLeftLattitude = Properties.Settings.Default.defaultCityBoundTopLeftLat;
            Properties.Settings.Default.cityBoundTopLeftLongitude = Properties.Settings.Default.defaultCityBoundTopLeftLng;
            Properties.Settings.Default.mapCenterLattitude = Properties.Settings.Default.defaultmapCenterLattitude;
            Properties.Settings.Default.mapCenterLongitude = Properties.Settings.Default.defaultmapCenterLongitude;
            Properties.Settings.Default.cityBoundBottomRightLattitude = Properties.Settings.Default.defaultcityBoundBottomRightLat;
            Properties.Settings.Default.cityBoundBottomRightLongitude = Properties.Settings.Default.defaultcityboundBottomRightLng;
            Properties.Settings.Default.fetchRecentStoriesInterval = Properties.Settings.Default.defaultFetchRecentStoriesInterval;
            Properties.Settings.Default.UserStoryLifetime = Properties.Settings.Default.defaultUserStoryLifetime;
            Properties.Settings.Default.XSize = Properties.Settings.Default.DefaultXSize;
            Properties.Settings.Default.YSize = Properties.Settings.Default.DefaultYSize;
            Properties.Settings.Default.messageQueueHostName = Properties.Settings.Default.defaultMessageQueueHostName;
            Properties.Settings.Default.messageQueueName = Properties.Settings.Default.defaultMessageQueueName;
            Properties.Settings.Default.MaxNumberMuseumStoriesToShowAtATime = Properties.Settings.Default.defaultMaxNumberMuseumStoriesToShowAtATime;
            Properties.Settings.Default.MaxNumberMyStoriesToShowAtATime = Properties.Settings.Default.defaultMaxNumberMyStoriesToShowAtATime;
            Properties.Settings.Default.OrientationPortrait = Properties.Settings.Default.DefaultOrientationPortrait;
            Properties.Settings.Default.ProjectorMode = Properties.Settings.Default.DefaultProjectorMode;

            Properties.Settings.Default.Save();
            populateFieldsWithCurrentValues();
            restartToTakeEffectReminder();
        }

        private void restartToTakeEffectReminder()
        {
            System.Windows.MessageBox.Show("Remember, for these changes to take effect you must restart the Application everywhere it is running");
        }

        private void txtMaxStories_PreviewKeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            //handle something here to make sure its all numbers
        }

        private void btnRestart_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();

            ////doesn't fully reload the mainwindow class

            //if (System.Windows.MessageBox.Show("Are you sure you want to restart?", "Restart Application", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            //{
            //    System.Windows.Application.Current.Exit += delegate(object sender2, ExitEventArgs e2)
            //    {
            //        System.Diagnostics.Process.Start(System.Windows.Application.ResourceAssembly.Location);
            //    };
            //    this.Close();
            //    System.Windows.Application.Current.Shutdown();

            //}
        }

        private void landscapeRadio_Checked(object sender, RoutedEventArgs e)
        {
            widthLbl.Content = "Screen Res. Width (Pixels)";
            heightLbl.Content = "Screen Res. Height (Pixels)";
        }

        private void portraitRadio_Checked(object sender, RoutedEventArgs e)
        {
            widthLbl.Content = "Screen Res. Height (Pixels)";
            heightLbl.Content = "Screen Res. Width (Pixels)";
        }

        private void CheckIfValidDouble(object sender)
        {
            try
            {
                Convert.ToDouble((sender as TextBox).Text);
                (sender as TextBox).Tag = "valid";
                (sender as TextBox).BorderBrush = Brushes.Black;
                (sender as TextBox).CaretBrush = Brushes.Black;
                (sender as TextBox).SelectionBrush = Brushes.Black;
                (sender as TextBox).BorderThickness = new Thickness(1);
            }
            catch
            {
                (sender as TextBox).Tag = "invalidDouble";
                (sender as TextBox).BorderBrush = Brushes.Red;
                (sender as TextBox).CaretBrush = Brushes.Red;
                (sender as TextBox).SelectionBrush = Brushes.Red;
                (sender as TextBox).BorderThickness = new Thickness(2);
            }
        }

        private void CheckIfValidInteger(object sender)
        {
            try
            {
                Convert.ToInt32((sender as TextBox).Text);
                (sender as TextBox).BorderBrush = Brushes.Black;
                (sender as TextBox).CaretBrush = Brushes.Black;
                (sender as TextBox).SelectionBrush = Brushes.Black;
                (sender as TextBox).BorderThickness = new Thickness(1);
                (sender as TextBox).Tag = "valid";
            }
            catch
            {
                (sender as TextBox).Tag = "invalidInteger";
                (sender as TextBox).BorderBrush = Brushes.Red;
                (sender as TextBox).CaretBrush = Brushes.Red;
                (sender as TextBox).SelectionBrush = Brushes.Red;
                (sender as TextBox).BorderThickness = new Thickness(2);
            }
        }

        private void txtMaxStories_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckIfValidInteger(sender);
        }

        private void maxMuseumStoriesOnScreenTxt_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckIfValidInteger(sender);
        }

        private void maxUserStoriesOnScreenTxt_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckIfValidInteger(sender);
        }

        private void maxRecentStoriesOnScreenTxt_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckIfValidInteger(sender);
        }

        private void txtStoryCycleInterval_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckIfValidInteger(sender);
        }

        private void dlNewStoriesIntervalTxtbox_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckIfValidInteger(sender);
        }

        private void userstorylifetimeTxtbox_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckIfValidInteger(sender);
        }

        private void txtMapMaxZoom_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckIfValidInteger(sender);
        }

        private void txtMapMinZoom_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckIfValidInteger(sender);
        }

        private void txtMapDefaultZoomLevel_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckIfValidInteger(sender);
        }

        private void screenHeightTxtbox_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckIfValidInteger(sender);
        }

        private void screenWidthTxtbox_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckIfValidInteger(sender);
        }

        private void scrlRightLngTxt_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckIfValidDouble(sender);
        }

        private void scrlBottomLatTxt_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckIfValidDouble(sender);
        }

        private void scrlLeftLngTxt_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckIfValidDouble(sender);
        }

        private void sclTopLatTxt_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckIfValidDouble(sender);
        }

        private void txtcityBoundBottomRightLongitude_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckIfValidDouble(sender);
        }

        private void txtcityBoundBottomRightLattitude_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckIfValidDouble(sender);
        }

        private void txtcityBoundTopLeftLongitude_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckIfValidDouble(sender);
        }

        private void txtcityBoundTopLeftLattitude_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckIfValidDouble(sender);
        }

        private void txtMapCenterLongitude_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckIfValidDouble(sender);
        }

        private void txtMapCenterLattitude_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckIfValidDouble(sender);
        }

        private void touchRadio_Checked(object sender, RoutedEventArgs e)
        {
            tableRadio.IsChecked = false;
            touchRadio.IsChecked = true;

            //textboxes
            txtMapMaxZoom.Visibility = Visibility.Visible;
            txtMapMinZoom.Visibility = Visibility.Visible;
            //scrlTopLatTxt.Visibility = Visibility.Visible;
            //scrlBottomLatTxt.Visibility = Visibility.Visible;
            //scrlLeftLngTxt.Visibility = Visibility.Visible;
            //scrlRightLngTxt.Visibility = Visibility.Visible;
            txtcityBoundTopLeftLattitude.Visibility = Visibility.Visible;
            txtcityBoundTopLeftLongitude.Visibility = Visibility.Visible;
            txtcityBoundBottomRightLattitude.Visibility = Visibility.Visible;
            txtcityBoundBottomRightLongitude.Visibility = Visibility.Visible;
            txtMaxStories.Visibility = Visibility.Visible;
            userstorylifetimeTxtbox.Visibility = Visibility.Visible;

            //labels
            //scrlRestrictionLbl.Visibility = Visibility.Visible;
            //scrlTopLatLbl.Visibility = Visibility.Visible;
            //scrlBottomLatLbl.Visibility = Visibility.Visible;
            //scrlLeftLngLbl.Visibility = Visibility.Visible;
            //scrlRightLngLbl.Visibility = Visibility.Visible;
            lblMapMaxZoom.Visibility = Visibility.Visible;
            lblMapMinZoom.Visibility = Visibility.Visible;
            lblcityBoundTopLeftLattitude.Visibility = Visibility.Visible;
            lblcityBoundTopLeftLongitude.Visibility = Visibility.Visible;
            lblcityBoundBottomRightLattitude.Visibility = Visibility.Visible;
            lblcityBoundBottomRightLongitude.Visibility = Visibility.Visible;
            lblMaxStories.Visibility = Visibility.Visible;
            userstorylifetimeLbl.Visibility = Visibility.Visible ;

            //rectangle5.Visibility = Visibility.Visible;
            rectangle3.Height = stackPanel1.Height + 8;
        }

        private void tableRadio_Checked(object sender, RoutedEventArgs e)
        {
            tableRadio.IsChecked = true;
            touchRadio.IsChecked = false;

            //textboxes
            txtMapMaxZoom.Visibility = Visibility.Collapsed;
            txtMapMinZoom.Visibility = Visibility.Collapsed;
            //scrlTopLatTxt.Visibility = Visibility.Collapsed;
            //scrlBottomLatTxt.Visibility = Visibility.Collapsed;
            //scrlLeftLngTxt.Visibility = Visibility.Collapsed;
            //scrlRightLngTxt.Visibility = Visibility.Collapsed;
            txtcityBoundTopLeftLattitude.Visibility = Visibility.Collapsed;
            txtcityBoundTopLeftLongitude.Visibility = Visibility.Collapsed;
            txtcityBoundBottomRightLattitude.Visibility = Visibility.Collapsed;
            txtcityBoundBottomRightLongitude.Visibility = Visibility.Collapsed;
            txtMaxStories.Visibility = Visibility.Collapsed;
            userstorylifetimeTxtbox.Visibility = Visibility.Collapsed;

            //labels
            //scrlRestrictionLbl.Visibility = Visibility.Collapsed;
            //scrlTopLatLbl.Visibility = Visibility.Collapsed;
            //scrlBottomLatLbl.Visibility = Visibility.Collapsed;
            //scrlLeftLngLbl.Visibility = Visibility.Collapsed;
            //scrlRightLngLbl.Visibility = Visibility.Collapsed;
            lblMapMaxZoom.Visibility = Visibility.Collapsed;
            lblMapMinZoom.Visibility = Visibility.Collapsed;
            lblcityBoundTopLeftLattitude.Visibility = Visibility.Collapsed;
            lblcityBoundTopLeftLongitude.Visibility = Visibility.Collapsed;
            lblcityBoundBottomRightLattitude.Visibility = Visibility.Collapsed;
            lblcityBoundBottomRightLongitude.Visibility = Visibility.Collapsed;
            lblMaxStories.Visibility = Visibility.Collapsed;
            userstorylifetimeLbl.Visibility = Visibility.Collapsed;

            //rectangle5.Visibility = Visibility.Collapsed;
            rectangle3.Height = stackPanel1.ActualHeight + 8;
        }
    }
}
